package com.example.jetour_line.map.mapview;

import android.content.Context;
import android.location.Location;
import android.util.Log;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;

import java.util.HashMap;
import java.util.Map;

public class BaseLocationUtil {
    //声明AMapLocationClient类对象
    private AMapLocationClient mlocationClient ;
    private AMapLocationClientOption mLocationOption;
    private LocationListener locationListener;
    public static String city;
    public static double lat;
    public static double lng;

    public static String getCity() {
        return city;
    }

    public void setLocationListener(LocationListener locationListener) {
        this.locationListener = locationListener;
    }

    public BaseLocationUtil(Context context){
        if(mlocationClient == null){
            try {
                mlocationClient = new AMapLocationClient(context);
                mLocationOption = new AMapLocationClientOption();
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //一次定位 （只返回一次定位信息）
//    public void startLocation(){
//        startLocation(true);
//    }
    //是否是一次定位 （只返回一次定位信息）
    public void startLocation(boolean isOncelocation,Context context){



        // 设置定位监听
        mlocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                Log.d("aMapLocation.toString()",aMapLocation.toString());
                int type = aMapLocation.getLocationType();
//                latitude=0.0#longitude=0.0#province=#coordType=GCJ02#city=#district=#cityCode=#adCode=#address=#country=#road=#poiName=#street=#streetNum=#aoiName=#poiid=#floor=#errorCode=20#errorInfo=模糊定位失败，具体可查看错误信息/详细信息描述#locationDetail=模糊权限下不支持连续定位#2006#pm111001#description=#locationType=0#conScenario=0
//                Log.d("aha_onLocationChanged",type+"");
                if(aMapLocation.getLatitude()<=0){
                    GPSLocationUtil gpsLocationUtil = new GPSLocationUtil(context);
                    gpsLocationUtil.getCurrentLocation(new GPSLocationUtil.OnLocationListener() {
                        @Override
                        public void onLocationReceived(Location location) {
                            // 在这里使用获取到的位置信息
                            double latitude = location.getLatitude();
                            double longitude = location.getLongitude();
                            if(locationListener != null){
                                Map map = new HashMap();
                                map.put("lat",latitude);
                                map.put("lng",longitude);
                                if(locationListener != null){
                                    locationListener.onLocationChanged(map);
                                }
                            }
                            // ...
                        }
                    });
                    return;
                }
                if(locationListener != null){
                    Map map = new HashMap();
                    map.put("type",type);
                    map.put("lat",aMapLocation.getLatitude());
                    map.put("lng",aMapLocation.getLongitude());
                    map.put("country",aMapLocation.getCountry());
                    map.put("title",aMapLocation.getAoiName());
                    map.put("province",aMapLocation.getProvince());
                    map.put("address",aMapLocation.getAddress());
                    map.put("street",aMapLocation.getStreet());
                    map.put("city",aMapLocation.getCity());
                    map.put("speed",aMapLocation.getSpeed());
                    map.put("altitude",aMapLocation.getAltitude());
                    map.put("bearing",aMapLocation.getBearing());
                    map.put("district",aMapLocation.getDistrict());
                    if(locationListener != null){
                        locationListener.onLocationChanged(map);
                    }
                }
            }
        });
        // 设置为高精度定位模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        // 定位场景 出行
//        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.Transport);
        //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
//        mlocationClient.stopLocation();
        // 只是为了获取当前位置，所以设置为单次定位
        mLocationOption.setOnceLocation(true);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(true);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(1000);
        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
//                mLocationOption.setHttpTimeOut(20000);
        //关闭缓存机制
//                mLocationOption.setLocationCacheEnable(false);
        // 设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        mlocationClient.startLocation();
    }

    // 获取距离
    public  static double getDistance(BaseLatLonPoint point1,BaseLatLonPoint point2){
        LatLng latLng1 = new LatLng(point1.getLatitude(),point1.getLongitude());
        LatLng latLng2 = new LatLng(point2.getLatitude(),point2.getLongitude());
        return AMapUtils.calculateLineDistance(latLng1,latLng2);
    }


    public interface LocationListener {
        void onLocationChanged(Map map);
    }

}